library(tidyverse)
library(lubridate)
library(pals)
library(plotly)
library(gganimate)
library(transformr)

grey_theme <- theme(axis.text.x = element_text(colour="grey20", size = 12, 
                                               angle = 90, hjust = 0.5, 
                                               vjust = 0.5),
                    axis.text.y = element_text(colour = "grey20", size = 12),
                    text=element_text(size = 16))

Challenge 1: Print a graph to .png using 3*ppi for the height and width and display the png file in the report using the above R Markdown format.

#Output plot to .png file
ppi <- 600
#Initiate plot as .png
png("images/time_series_example_plot.png", width=8*ppi, height=8*ppi, res=ppi)
ggplot(dailydeaths, aes(Date, Deaths)) +
  geom_point(color="red", alpha=0.8) +
  geom_line(color="red", alpha=0.8)+
  labs(title = "Total COVID deaths worldwide by day",
       x = "Date",
       y = "Total deaths") +
  theme(plot.title = element_text(hjust = 0.5)) +
  grey_theme
dev.off()
Total COVID deaths by day

Total COVID deaths by day

Challenge 2: Turn one of the exercises from Lab 5 into an interactive graph with plotyly

#TS plot
plot <- 
  ggplot(deathTS, aes(Date, Deaths, color=Country)) +
  #geom_point(color="red", alpha=0.8) +
  geom_line(alpha=0.8, size=1.0)+
    labs(title = "Daily COVID deaths (10 highest totals)",
       x = "Date",
       y = "Total deaths") +
  scale_color_manual(values=as.vector(pals::glasbey(32)), name = "Country") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.title = element_blank()) +
  grey_theme
ggplotly(plot) %>% 
  layout(legend = list(x=100, y=0.5))

Challenge 3. Create an animated graph of your choosing using the time series data to display an aspect (e.g. states or countries) of the data that is important to you.

#Plot
p <-
  ggplot(USratio, aes(Date, ratio)) +
    geom_area(color="blue", fill = "blue", alpha= 0.4) +
labs(title = "US deaths to confirmed cases ratio by day",
     x = "Date",
     y = "Deaths:Confirmed cases") +
  theme(plot.title = element_text(hjust = 0.5)) +
  grey_theme +
  transition_reveal(Date)
animate(p, end_pause = 20)

```